﻿<%@ WebHandler Language="C#" Class="HandlerCompare" %>

using System;
using System.Web;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;

public class HandlerCompare : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        string status = context.Request.QueryString["status"];
        switch (status)
        {
            case "getProductsInfo":
                string[] buffsInfo;
                int[] prosId;
                buffsInfo = context.Request.QueryString["products"].Split(',');
                prosId = Array.ConvertAll(buffsInfo, int.Parse);
                LinkToDatabase ldblnfo = new LinkToDatabase();
                List<Product> ps = ldblnfo.GetProductsInfo(prosId);
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(ps));
                break;
            case "addItemToCompareBox":
                int productId;
                if (int.TryParse(context.Request.QueryString["productId"], out productId)) 
                {
                    LinkToDatabase ldb = new LinkToDatabase();
                    ProductPreview pp = ldb.GetProductPreview(productId);
                    context.Response.ContentType = "application/json";
                    context.Response.Write(JsonConvert.SerializeObject(pp));
                }
                break;
            case "loadCompareBoxItem":
                string[] buff;
                int[] productsId;
                buff = context.Request.QueryString["products"].Split(',');
                productsId = Array.ConvertAll(buff, int.Parse);
                LinkToDatabase ldbl = new LinkToDatabase();
                List<ProductPreview> psp = ldbl.GetProductsPreview(productsId);
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(psp));
                break;
            case "getRefer":
                string[] buffRef;
                int[] prosIdRef;
                buffRef = context.Request.QueryString["products"].Split(',');
                prosIdRef = Array.ConvertAll(buffRef, int.Parse);
                prosIdRef = prosIdRef.OrderBy(i => i).ToArray();
                LinkToDatabase ldblRef = new LinkToDatabase();
                List<ProductPreview> pspRef = ldblRef.GetProductsRefer(prosIdRef);
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(pspRef));
                break;
            case "saveCompareRecord":
                string[] buffRecord;
                int[] prosIdRecord;
                buffRecord = context.Request.QueryString["products"].Split(',');
                prosIdRecord = Array.ConvertAll(buffRecord, int.Parse);
                prosIdRecord = prosIdRecord.OrderBy(i => i).ToArray();
                LinkToDatabase ldbRecord = new LinkToDatabase();
                ldbRecord.SaveCompareRecord(prosIdRecord);
                context.Response.ContentType = "data/text";
                context.Response.Write("success");
                break;
            case "getSelectCategory":
                CategoryAndBrand c = new CategoryAndBrand();
                IEnumerable<Category> cates = c.getCategory();
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(cates));
                break;
            case "getSelectBrand":
                CategoryAndBrand b = new CategoryAndBrand();
                IEnumerable<Brand> brands = b.getBrand();
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(brands));
                break;
            case "getSelectProduct":
                string brandID, cateID;
                brandID = context.Request.QueryString["brandID"];
                cateID = context.Request.QueryString["cateID"];
                CategoryAndBrand p = new CategoryAndBrand();
                IEnumerable<ProductS> pros = p.getProduct(brandID, cateID);
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(pros));
                break;
        }
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }
}